<?php
    //万能表单
class dbt_actPub extends sing_table {
    private $_mid;
    private $_G;
    private $_openid;
    private $_memberid;
    private $_moretype;
    private $_memberWhere;
    
    function __construct(){
        global $_G;
        $this->_G = &$_G;
        $this->_mid = $_GET['mid'];
        $thisMember = C::t("#api#hotel")->createMember();
        if($thisMember){
            $this->_openid = $thisMember['openid'];
            $this->_memberid = $thisMember['memberid'];
        }
        $this->_moretype = array(3, 4, 5);
        $this->_memberWhere = C::t("#api#user")->memberWhere($this->_openid,$this->_memberid);
    }
    
    //加载会员表单
    public function form($tablename,$tableid){
        if(!$tablename || !$tableid){
            return array("Error"=>1,"Msg"=>"参数错误");
        }
        //表单项
        DB::SetMainDB(DB_MAIN);
        $act = DB::fetch_all('select id,type,name,ismust,pubid from %t where mid=%s and tablename=%s and tableid=%s and parentid=0 and display=1 order by turn desc,id asc',array('wx_act',$this->_mid,$tablename,$tableid));
        //遍历选项
        foreach($act as $k => $v){
            //如果选项是 带元素的 查找元素
            if(in_array($v['type'],$this->_moretype)){
                DB::SetMainDB(DB_MAIN);
                $act[$k]['option'] = DB::fetch_all('select id,name from %t where mid=%s and parentid=%d and display=1 order by turn desc,id asc',array('wx_act',$this->_mid,$v['id']));
            }else{
                $act[$k]['option'] = array();
            }
            $act[$k]['ext']= array();
            if($v['pubid'] && $this->_openid){
                //没有logid 但是有公共的act 查找公共的ext 表数据 作为默认值 
                DB::SetMainDB(DB_MAIN);
                $extarr = DB::fetch_first('select id,value from %t where mid=%s and openid=%s and  actpid=%d and display=1 order by `usage` desc,id desc',array('wx_act_ext_pub',$this->_mid,$this->_openid,$v['pubid']));
                //如果 选项类型是 带元素的
                if(in_array($v['type'],$this->_moretype)){
                    //如果有公共记录值
                    if($extarr['value']){
                        $extarr_value = explode('<|!|>',$extarr['value']);
                        //遍历元素 判断 哪个选项是默认的选择的
                        foreach($act[$k]['option'] as $k_op=>$v_op){
                            if(in_array($v_op['name'],$extarr_value)){
                                $act[$k]['ext'][$v_op['id']] = array('id'=>0,'actid'=>$v_op['id'],'value'=>$v_op['name']);
                            }
                        }
                    }
                }else{
                    //如果不是带元素的 直接赋值
                    $act[$k]['ext'][$v['id']] = $extarr;
                }

            }
        }
        
        return array("Error"=>0,"Msg"=>"数据获取成功","data"=>$act);

    }
    //添加信息
    public function act_order($tablename,$tableid,$ordername,$orderid,$info){ //$tablename：主表名称，$tableid：主表id，$ordername：记录表名称，$orderid：记录表id，$info：数据
        if(!$tablename || !$tableid || !$ordername || !$orderid || !$info){
            return array("Error"=>1,"Msg"=>"参数错误");
        }
        //初始化数据
        $insert = array();
        $public = array();
        //查找选项
        DB::SetMainDB(DB_MAIN);
        $act = DB::fetch_all('select id,type,name,ismust,pubid from %t where mid=%s and tablename=%s and tableid=%s and parentid=0 and display=1 order by turn desc,id asc',array('wx_act',$this->_mid,$tablename,$tableid));
        //遍历选项
        foreach($act as $k => $v){
            $value = $info['act'][$v['id']];
            //判断必填
            if($v['ismust'] && empty($value)){
                return array('Error' => 1,'Msg' =>$v['name'].' 为必填选项');
            }
            //判断 选项类型 是不是需要元素
            if(in_array($v['type'],$this->_moretype)){
                //如果没有值则不添加数据
                if(empty($value)) continue;
                //判断值是不是数组  
                if(is_array($value)){
                    //此处 类型为多选
                    $pubvaluearr = array();
                    foreach($value as $k_v=>$v_v){
                        DB::SetMainDB(DB_MAIN);
                        $option = DB::fetch_first('select id,name,pubid from %t where mid=%s and parentid=%d and id=%d and display=1 ',array('wx_act',$this->_mid,$v['id'],$v_v));	
                        //初始化 需要添加的数组
                        $insert[$v_v] = array();
                        $insert[$v_v]['value'] = $option['name'];
                        $insert[$v_v]['actid'] = $v_v;
                        $insert[$v_v]['actpid'] = $v['id'];	
                        $pubvaluearr[] = $option['name'];

                    }
                    $public[$k] = array();
                    $public[$k]['value'] = implode('<|!|>',$pubvaluearr);
                    $public[$k]['actpid'] = $v['pubid'];
                    $public[$k]['actid'] = 0;	
                }else{
                    //此处 类型为单选 和 下拉
                    DB::SetMainDB(DB_MAIN);
                    $option = DB::fetch_first('select id,name,pubid from %t where mid=%s and parentid=%d and id=%d and display=1 ',array('wx_act',$this->_mid,$v['id'],$value));
                    //初始化 需要添加的数组
                    $insert[$value] = array();
                    $insert[$value]['value'] = $option['name'];
                    $insert[$value]['actid'] = $value;
                    $insert[$value]['actpid'] = $v['id'];

                    $public[$k] = array();
                    $public[$k]['value'] = $option['name'];
                    $public[$k]['actpid'] = $v['pubid'];
                    $public[$k]['actid'] = 0;
                }

            }else{
                //初始化 需要添加的数组
                $insert[$k] = array();
                //此处类型为文本框和文本域
                $insert[$k]['value'] = $value;
                $insert[$k]['actid'] = $v['id'];
                $insert[$k]['actpid'] = $v['id'];

                $public[$k] = array();
                $public[$k]['value'] = $value;
                $public[$k]['actpid'] = $v['pubid'];
                $public[$k]['actid'] = $v['pubid'];
            }
        }
        //最后一次判断是否有logid
        $insert_log = array();
        $insert_log['_uid'] = $this->_G['_uid'];
        $insert_log['uid'] = $this->_G['uid'];
        $insert_log['mid'] = $this->_mid;
        $insert_log['openid'] = $this->_openid;
        $insert_log['member_id'] = $this->_memberid;
        $insert_log['tablename'] = $ordername;
        $insert_log['tableid'] = $orderid;
        $insert_log['tableparentid'] = 0;
        $insert_log['inputtime'] = TIME;
        $insert_log['status'] = 1;
        $insert_log['display'] = 1;
        DB::SetMainDB(DB_MAIN);
        $insert_id = DB::insert('wx_act_log',$insert_log,true);

        foreach($insert as $k=>$v){
            $insert_ext = array();
            $insert_ext = $v;
            $insert_ext['openid'] = $this->_openid;
            $insert_ext['member_id'] = $this->_memberid;
            DB::SetMainDB(DB_MAIN);
            $extid = DB::result_first('select id from %t where mid=%s and logid=%d and actid=%d and display=1',array('wx_act_ext',$this->_mid,$insert_id,$k));
            if($extid){
                $insert_ext['updatetime'] = TIME;
                DB::SetMainDB(DB_MAIN);
                DB::update('wx_act_ext',$insert_ext,array('mid'=>$this->_mid,'id'=>$extid));
            }else{
                $insert_ext['logid'] = $insert_id; 
                $insert_ext['inputtime'] = TIME;
                $insert_ext['mid'] = $this->_mid;
                $insert_ext['tablename'] = $ordername;
                $insert_ext['tableid'] = $orderid;
                $insert_ext['status'] = 1;
                $insert_ext['display'] = 1;
                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_act_ext',$insert_ext,true);
            }
        }

        if(($this->_openid || $this->_memberid) && $insert_id){
            $this->form_pub($public);
        }
        
        return array("Error"=>0,"Msg"=>"信息录入成功，请等待审核");
    }
    //存储公共的表单
    private function form_pub($public=array()){
        if((!$this->_openid && !$this->_memberid) || empty($public)) return false;
        foreach($public as $k=>$v){
            if(empty($v['actpid']) || empty($v['value'])) continue;
            $insert = array();
            DB::SetMainDB(DB_MAIN);
            $pub = DB::fetch_first('select id,`usage` from %t where mid=%s %i and actpid=%d and value=%s and display=1',array('wx_act_ext_pub',$this->_mid,$this->_memberWhere,$v['actpid'],$v['value']));
            if($pub['id']){
                $insert['usage'] = $pub['usage'] + 1;
                $insert['updatetime'] = TIME;
                DB::SetMainDB(DB_MAIN);
                DB::update('wx_act_ext_pub',$insert,array('mid'=>$this->_mid,'id'=>$pub['id']));
            }else{
                $insert = $v;
                $insert['mid'] = $this->_mid;
                $insert['openid'] = $this->_openid;
                $insert['member_id'] = $this->_memberid;
                $insert['usage'] = 1;
                $insert['inputtime'] = TIME;
                $insert['status'] = 1;
                $insert['display'] = 1;
                DB::SetMainDB(DB_MAIN);
                DB::insert('wx_act_ext_pub',$insert,true);
            }
            $insert = array();
        }
    }
}
?>